import scrapy
import json
from csgo.items import CsgoItem
class GoSpider(scrapy.Spider):
    name = 'go'
    #allowed_domains = ['csgo.com']
    #start_urls = ['http://csgo.com/']
    def start_requests(self):
        for i in range(1,765):
            url = 'https://buff.163.com/api/market/goods?game=csgo&page_num='+str(i)
            yield scrapy.Request(url=url,callback=self.parse)
    def parse(self, response):
        csgo = CsgoItem()
        jd = json.loads(response.text)
        for i in jd.get('data').get('items'):
            try:
                csgo['name'] = i.get('name')
                csgo['jia'] = i.get('sell_min_price')
                csgo['c_jia'] = i.get('sell_reference_price')
                csgo['shu'] = i.get('sell_num')
                csgo['z_jia'] = i.get('buy_max_price')
                csgo['g_shu'] = i.get('buy_num')
                csgo['mo'] = i.get('goods_info').get('info').get('tags').get('exterior').get('localized_name')
                csgo['zhi'] = i.get('goods_info').get('info').get('tags').get('quality').get('localized_name')
                csgo['lei'] = i.get('goods_info').get('info').get('tags').get('rarity').get('localized_name')
                csgo['qiang'] = i.get('goods_info').get('info').get('tags').get('type').get('localized_name')
                csgo['q_name']=i.get('goods_info').get('info').get('tags').get('weapon').get('localized_name')
                yield csgo
            except:
                csgo['name'] = i.get('name')
                csgo['jia'] = i.get('sell_min_price')
                csgo['c_jia'] = i.get('sell_reference_price')
                csgo['shu'] = i.get('sell_num')
                csgo['z_jia'] = i.get('buy_max_price')
                csgo['g_shu'] = i.get('buy_num')
                csgo['zhi'] = i.get('goods_info').get('info').get('tags').get('quality').get('localized_name')
                csgo['lei'] = i.get('goods_info').get('info').get('tags').get('rarity').get('localized_name')
                csgo['qiang'] = i.get('goods_info').get('info').get('tags').get('type').get('localized_name')
                yield csgo
                print(1)
